/*  -*- mode: c++; coding: utf-8; c-file-style: "stroustrup"; -*-

    Copyright 2008 Asier Aguirre <asier.aguirre@gmail.com>
    This file is part of Intromit.

    Intromit is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Intromit is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Intromit.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <stdlib.h>
#include "util.hh"

// LOGGER CLASS

FILE *logger::file()
{
#ifndef LOG_USE_STDERR
    static FILE *logfile=NULL;
    if(logfile)
    {
#ifndef LOG_NOSYNC
	fflush(logfile);
#endif
	return logfile;
    }
    else if((logfile=fopen(LOG_FILENAME, "w"))) return logfile;
    else
    {
	fprintf(stderr, "fopen log output\n");
	return logfile=stderr;
    }
#else
    return stderr;
#endif
}

unsigned util::fast_checksum(void *data, int n_data)
{
    unsigned c=n_data<<4;
    int i;
    for(i=0; i<n_data-(int)sizeof(unsigned); i+=4)
	c+=*(unsigned*)&((char*)data)[i];
    for(; i<n_data; i++)
	c+=((char*)data)[i]<<12;
    return c;
}


// DEBUG
#ifdef DEBUG_CC
int dbg_mem_heap=0;
#endif
